package com.example.libraryapp.book;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.example.libraryapp.R;
import com.example.libraryapp.db.MySqliteHelper;
import com.example.libraryapp.util.Constance;

/**
 * 图书服务，主要提供对图书信息的管理
 */
public class BookService {
	
	/**
	 * 图书信息加入到数据库中
	 */
	public static void addBookInfo(Context context){
		try {
			//构造参数
			ContentValues values = new ContentValues();
			values.put("imageid", R.drawable.first_code);
			values.put("name", "第一行代码");
			values.put("author", "郭林");
			values.put("publishing", "人民邮电出版社");
			
			ContentValues values2 = new ContentValues();
			values2.put("imageid", R.drawable.crazy_android);
			values2.put("name", "Android疯狂讲义");
			values2.put("author", "李刚");
			values2.put("publishing", "电子工业出版社");
			
			ContentValues[] valuesArr = new ContentValues[]{values, values2};
			MySqliteHelper.getInstance(context).insert(Constance.BOOK_TABLE_NMAE, null, valuesArr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 得到所有的图书信息信息
	 * @return
	 */
	public static List<BookInfo> getAllBook(Context context) {
		List<BookInfo> bookList = null;
		Cursor cursor = MySqliteHelper.getInstance(context).query(Constance.BOOK_TABLE_NMAE, "1=1");
		if (cursor != null) {
			bookList = new ArrayList<BookInfo>();
			while(cursor.moveToNext()){
				BookInfo bookInfo = new BookInfo();
				bookInfo.setId(cursor.getString(cursor.getColumnIndex("id")));
				bookInfo.setImageId(cursor.getInt(cursor.getColumnIndex("imageid")));
				bookInfo.setName(cursor.getString(cursor.getColumnIndex("name")));
				bookInfo.setAuthor(cursor.getString(cursor.getColumnIndex("author")));
				bookInfo.setPublishing(cursor.getString(cursor.getColumnIndex("publishing")));
				bookList.add(bookInfo);
			}
		}
		return bookList;
	}
	
	/**
	 * 根据条件得到图书信息
	 * @return
	 */
	public static List<BookInfo> getBookInfoByParam(Context context, String param) {
		List<BookInfo> bookList = null;
		String where = "name like '%" + param + "%'";
		where += " or ";
		where += "author like '%" + param + "%'";
		where += " or ";
		where += "publishing like '%" + param + "%'";
		Cursor cursor = MySqliteHelper.getInstance(context).query(Constance.BOOK_TABLE_NMAE, where);
		if (cursor != null) {
			bookList = new ArrayList<BookInfo>();
			while(cursor.moveToNext()){
				BookInfo bookInfo = new BookInfo();
				bookInfo.setId(cursor.getString(cursor.getColumnIndex("id")));
				bookInfo.setImageId(cursor.getInt(cursor.getColumnIndex("imageid")));
				bookInfo.setName(cursor.getString(cursor.getColumnIndex("name")));
				bookInfo.setAuthor(cursor.getString(cursor.getColumnIndex("author")));
				bookInfo.setPublishing(cursor.getString(cursor.getColumnIndex("publishing")));
				bookList.add(bookInfo);
			}
		}
		return bookList;
	}
	
	/**
	 * 根据条件得到图书信息
	 * @return
	 */
	public static BookInfo find(Context context, String bookId) {
		BookInfo book = null;
		String where = "id = '" + bookId + "'";
		Cursor cursor = MySqliteHelper.getInstance(context).query(Constance.BOOK_TABLE_NMAE, where);
		if (cursor != null) {
			while(cursor.moveToNext()){
				book = new BookInfo();
				book.setId(cursor.getString(cursor.getColumnIndex("id")));
				book.setImageId(cursor.getInt(cursor.getColumnIndex("imageid")));
				book.setName(cursor.getString(cursor.getColumnIndex("name")));
				book.setAuthor(cursor.getString(cursor.getColumnIndex("author")));
				book.setPublishing(cursor.getString(cursor.getColumnIndex("publishing")));
			}
		}
		return book;
	}
	/**
	 * 删除表
	 * @param context
	 */
	public static void clear(Context context) {
		MySqliteHelper.getInstance(context).clearCache(Constance.BOOK_TABLE_NMAE);
	}
}
